import axios from '~/plugins/axios';

import * as types from '../../mutation-types/power-setting/index';

//查询模块下资源。没有模块的id就查询全部的资源
/**
 *
 * @param start
 * @param size
 * @param appModuleResourceId
 * @param useLevel
 * @param approveLevel
 * @param name
 */
export const powerSettingList = function({commit, state}, preload) {
    //axios.get('http://localhost:8000/mock-data/power-setting/power-list'+preload.page+'.json',{
    axios.get(process.env.baseUrl + '/appmoduleresource/list', {
        params: {
            start: preload.page,
            size: preload.size,
            appModuleId: preload.appModuleId,
            useLevel: preload.useLevel,
            approveLevel: preload.approveLevel,
            name: preload.name
        }
    })
        .then(function(response) {
            if (!response) {
                return;
            }
            if (response.data.code == 0) {
                commit(types.POWER_SETTING_LIST, {
                    powerSettingList: response.data.data.list
                });
                commit(types.POWER_SETTING_LIST_CURRENTPAGE, {
                    currentSettingPage: (response.data.data.pageNum == 0) ? 1 : response.data.data.pageNum
                });
                commit(types.POWER_SETTING_LIST_TOTALCOUNT, {
                    totalSettingCount: response.data.data.total
                });
            }
        })
};

//请求模块列表
export const powerModuleList = function({commit, state}, preload) {
    //axios.get('http://localhost:8000/mock-data/power-setting/list-page'+preload.page+'.json',{
    axios.get(process.env.baseUrl + '/appmodule/list', {
        params: {
            appId: preload.appId,
            start: preload.page,
            size: preload.size
        }
    })
        .then(function(response) {
            if (!response) {
                return;
            }
            if (response.data.code == 0) {
                commit(types.POWER_MOUDLE_LIST, {
                    powerModuleList: response.data.data.list
                });
                commit(types.POWER_MOUDLE_LIST_CURRENTPAGE, {
                    currentModulePage: (response.data.data.pageNum == 0) ? 1 : response.data.data.pageNum
                });
                commit(types.POWER_MOUDLE_LIST_TOTALCOUNT, {
                    totalModuleCount: response.data.data.total
                });
            }
        })
};

//添加模块/修改模块
export const addPowerModule = function({commit, state}, preload) {
    if (preload.id) {
        //如果有id是修改
        return axios.post(process.env.baseUrl + '/appmodule/update', preload)
    } else {
        //没有id是添加
        return axios.post(process.env.baseUrl + '/appmodule/insert', preload)
    }
};

//删除模块
export const delPowerModule = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/appmodule/delete', preload);
};

//添加权限
export const addPowerItem = function({commit, state}, preload) {
    if (preload.id) {
        //如果有id是修改
        return axios.post(process.env.baseUrl + '/appmoduleresource/update', preload)
    } else {
        //没有id是添加
        return axios.post(process.env.baseUrl + '/appmoduleresource/insert', preload)
    }
};

//删除权限
export const delPowerItem = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/appmoduleresource/delete', {
        id: preload.powerId
    });
};

//删除权限
export const getTreeList = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/department/listTree', preload || {});
};

/**
 * 获取岗位信息分页
 *
 * @param commit
 * @param state
 * @param preload
 */
export const getPostList = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/job/jobPage', preload || {})
};

/**
 * 添加岗位信息
 *
 * @param commit
 * @param state
 * @param preload
 */
export const addPost = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/job/addJob', preload || {})
};

/**
 * 修改岗位信息
 *
 * @param commit
 * @param state
 * @param preload
 */
export const updatePost = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/job/updateJob', preload || {})
};

/**
 * 删除岗位信息
 *
 * @param commit
 * @param state
 * @param preload
 */
export const deletePost = function({commit, state}, preload) {
    return axios.post(process.env.baseUrl + '/job/deleteJob', preload || {})
};